# -*- coding: UTF-8 -*-
import os
import configparser
from utils.config_utils import parse_str_with_star

conf = configparser.ConfigParser()
cur_path = os.path.dirname(os.path.realpath(__file__))
file_path = '../config/app.cfg'

secret_key = ""


def redis_host():
    return conf.get("redis", "redis_host")


def redis_port():
    return conf.getint("redis", "redis_port")


def user_edit():
    return conf.get('redis_key', "user_edit")


def expire_time():
    return conf.get('redis', 'expire_time')


def set_time_edit():
    return conf.get('redis_key', 'set_time_edit')


def time_results():
    return conf.get('redis_key', 'time_results')


def __init_configloader():
    global config_path
    cur_path = os.path.dirname(os.path.realpath(__file__))
    config_path = os.path.join(cur_path, file_path)
    conf.read(config_path)


def reload_conf_file():
    """
    重新加载一下配置文件
    :return:
    """
    cur_path = os.path.dirname(os.path.realpath(__file__))
    config_path = os.path.join(cur_path, file_path)
    conf.read(config_path)


def detection_interval():
    """
    获取检测时间，即多久检测一次
    :return:
    """
    return parse_str_with_star(conf.get("config", "detection_interval"))


def warning_time():
    """
    获取告警阈值时间，超过该阈值告警
    :return:
    """
    return parse_str_with_star(conf.get("config", "warning_time"))


def set_value(section, option, value):
    """
    设置cfg的值,不允许新建新的option
    :param session:
    :param option:
    :param value:
    :return:
    """
    if conf.has_option(section=section, option=option):
        conf.set(section, option, value)
    else:
        raise Exception(option + " do not exists,please check it!")


def set_values(section, option_begin, values):
    """
    设置session中option的值。
    :param section: 需要设置的session
    :param option_begin: 开头编号
    :param values: 传递进来一个数组
    :return:
    """
    conf.remove_section(section)  # 先删除session
    conf.add_section(section)  # 再新建session
    for num, value in enumerate(values):
        conf.set(section, option_begin + str(num), str(value))


def save_change():
    """
    执行保存更改
    :return:
    """
    with open(config_path, 'w') as fw:  # 循环写入
        conf.write(fw)


def get_value_by_section(section):
    """
    根据section找出所有下面的options
    :param section:
    :return:
    """
    result = {}
    options = conf.options(section)
    for option in options:
        value = conf.get(section=section, option=option)
        result[option] = value
    return result


def get_secret_key():
    return conf.get("secret", 'key')
    # """
    # 获取加密信息
    # :return:
    # """
    # if secret_key == "":
    #     with open(secret_path, 'r') as f:
    #         key = f.read()
    #     secret_key = key
    #     return secret_key
    # return secret_key


def get_time():
    return {
        "work_time": conf.get("config", "work_time"),
        "noon_sleep": conf.get("config", "noon_sleep"),
        "break_off": conf.get("config", "break_off")

    }


def get_options(section):
    """
    根据section获取options
    :param section:
    :return:
    """
    return conf.options(section)


def get_data_api_url():
    """
    获取日期的api
    :return:
    """
    return conf.get("third_url", "data_api")


def get_rtsp_api_url():
    """
    获取rtsp的地址
    :return:
    """
    return conf.get("third_url", "rtsp_api")


def get_post_message_api_url():
    """
    :return:
    """
    return conf.get("third_url", "post_message_api")


__init_configloader()

if __name__ == '__main__':
    pass
